package com.shangma.cn.common.perm;

import com.shangma.cn.common.exception.ApiException;
import com.shangma.cn.common.http.AxiosStatus;
import com.shangma.cn.components.TokenService;
import com.shangma.cn.entity.Menu;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.mapstruct.BeforeMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;

/**
 * 开发者：辉哥
 * 特点： 辉哥很帅
 * 开发时间：2021/7/1 17:28
 * 文件说明：
 */
@Aspect
@Component
public class PermAop {


    @Autowired
    private TokenService tokenService;

    @Before("pointCut()")
    public void checkBtnPrem(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        HasPerm annotation = method.getAnnotation(HasPerm.class);
        if (annotation != null) {
            String s = annotation.permSign();
            System.out.println(s);
            List<Menu> menus = tokenService.getLoginAdminCache().getMenus();
            boolean b = menus.stream().anyMatch(menu -> s.equalsIgnoreCase(menu.getPermSign()));
            System.out.println(b);
            if (!b) {
                throw new ApiException(AxiosStatus.NO_PERM);
            }
        }
    }
    @Pointcut("execution(* com.shangma.cn.controller.*.*(..))")
    public void pointCut() {

    }
}
